//
//  FirstLandViewController.m
//  FoodCorner
//
//  Created by lanou3g on 16/3/8.
//  Copyright © 2016年 zhangchao. All rights reserved.
//

#import "FirstLandViewController.h"
#import <AVOSCloud.h>
#import <AVOSCloudSNS.h>
#import <Masonry.h>
#import "UserViewController.h"
#import "RegisterViewController.h"
#import "AppDelegate.h"
#import "UserDataHelper.h"
#import <RongIMKit/RongIMKit.h>
#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height

@interface FirstLandViewController ()<UITextFieldDelegate>

@property (nonatomic,strong) UITextField *userNameTF;//用户名
@property (nonatomic,strong) UITextField *userPwdTF;//密码
@property (nonatomic,strong) UIButton *loginBtn;//登录按钮
@property (nonatomic,strong) UIButton *registerBtn;//注册按钮
@property (nonatomic,strong) UIButton *retrievePwdBtn;//重置密码
@property (nonatomic,strong) UIButton *qqBtn;//第三方QQ
@property (nonatomic,strong) UIButton *wxBtn;//第三方微信

@end

@implementation FirstLandViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.tabBarController.tabBar.hidden = YES;
    [self addViews];
    self.navigationItem.title = @"登录";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(didClickLeftBtn:)];
    
}

- (void)addViews{
    
    self.view.backgroundColor = [UIColor greenColor];
    self.userNameTF = [[UITextField alloc]init];
    self.userNameTF.placeholder = @"请输入用户名";
    self.userNameTF.layer.borderWidth = 0.5;
    self.userNameTF.layer.cornerRadius = 5;
    self.userNameTF.delegate = self;
    [self.view addSubview:self.userNameTF];
    [self.userNameTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kHeight *0.15);
        make.left.equalTo(self.view).offset(kWidth *0.05);
        make.right.equalTo(self.view).offset(-(kWidth *0.05));
        make.height.equalTo(@40);
    }];
    self.userPwdTF = [[UITextField alloc] init];
    self.userPwdTF.placeholder = @"请输入密码";
    self.userPwdTF.layer.borderWidth = 0.5;
    self.userPwdTF.layer.cornerRadius = 5;
    self.userPwdTF.secureTextEntry = YES;
    self.userPwdTF.delegate = self;
//    self.userPwdTF.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.userPwdTF];
    [self.userPwdTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kHeight *0.22);
        make.left.equalTo(self.view).offset(kWidth *0.05);
        make.right.equalTo(self.view).offset(-(kWidth *0.05));
        make.height.equalTo(@40);
    }];
    self.loginBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [self.loginBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.loginBtn.layer.cornerRadius = 15;
    self.loginBtn.layer.borderWidth = 0.5;
    [self.loginBtn addTarget:self action:@selector(didClickLoginBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_loginBtn];
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kHeight *0.3);
        make.left.equalTo(self.view).offset(kWidth *0.05);
        make.right.equalTo(self.view).offset(-(kWidth *0.05));
        make.height.equalTo(@40);
    }];
    self.retrievePwdBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.retrievePwdBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
    [self.retrievePwdBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.retrievePwdBtn addTarget:self action:@selector(didClickRetrievePwdBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.retrievePwdBtn];
    [self.retrievePwdBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kHeight *0.4);
        make.left.equalTo(self.view).offset(kWidth *0.1);
        make.width.equalTo(@(kWidth *0.3));
        make.height.equalTo(@30);
    }];
    
    self.registerBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [self.registerBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.registerBtn addTarget:self action:@selector(didClickRegisterBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.registerBtn];
    [self.registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kHeight * 0.4);
        make.left.equalTo(self.view).offset(kWidth * 0.7);
        make.width.equalTo(@(kWidth * 0.2));
        make.height.equalTo(@30);
    }];
    
    UILabel *label = [[UILabel alloc] init];
    [self.view addSubview:label];
    label.text = @"快捷登录";
    label.font = [UIFont systemFontOfSize:15];
    label.textAlignment = NSTextAlignmentCenter;

    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.equalTo(self.view).offset(kHeight * 0.5);
        make.size.mas_equalTo(CGSizeMake(kWidth * 0.2, 30));
    }];
    
    self.qqBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.qqBtn setImage:[UIImage imageNamed:@"Share_QQ"] forState:UIControlStateNormal];
    [self.view addSubview:self.qqBtn];
    [self.qqBtn addTarget:self action:@selector(qqAction) forControlEvents:UIControlEventTouchUpInside];

    [self.qqBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kHeight * 0.6);
        make.left.equalTo(self.view).offset(kWidth * 0.2);
        make.size.mas_equalTo(CGSizeMake(kWidth * 0.13, kWidth * 0.13));
    }];
    
    
    self.wxBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.wxBtn setImage:[UIImage imageNamed:@"Share_WeChat"] forState:UIControlStateNormal];
    [self.view addSubview:self.wxBtn];
    [self.wxBtn addTarget:self action:@selector(wxAction) forControlEvents:UIControlEventTouchUpInside];
    [self.wxBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kHeight * 0.6);
        make.right.equalTo(self.view).offset(-(kWidth * 0.2));
        make.size.mas_equalTo(CGSizeMake(kWidth * 0.13, kWidth * 0.13));
    }];
    
}
//左边按钮的点击事件
- (void)didClickLeftBtn:(UIBarButtonItem *)sender
{
    
    [self.navigationController popViewControllerAnimated:YES];
}

//登录按钮点击事件
- (void)didClickLoginBtn
{
    [AVUser logInWithUsernameInBackground:self.userNameTF.text password:self.userPwdTF.text block:^(AVUser *user, NSError *error) {
        if (user) {
        UserViewController *userVC = [[UserViewController alloc]init];
       AppDelegate *dele = [UIApplication sharedApplication].delegate;
            dele.didLogin = YES;
            [[UserDataHelper shareInstance] getToken];
            
//            //即时通讯登陆
//            [[RCIM sharedRCIM] initWithAppKey:@"m7ua80gbu90qm"];
//            NSString *token = [UserDataHelper shareInstance].token;
//            [[RCIM sharedRCIM] connectWithToken:token success:^(NSString *userId) {
//                NSLog(@"登录成功");
//            } error:^(RCConnectErrorCode status) {
//                NSLog(@"登录失败，失败代码为：%ld",status);
//            } tokenIncorrect:^{
//                NSLog(@"登录失败，token出错");
//            }];
            
            if (self.navigationController) {
                
                [self.navigationController popToViewController:userVC animated:YES];
            }else{
                
                [self dismissViewControllerAnimated:YES completion:nil];
            }
            
        }else{
            
            NSLog(@"用户名或者密码错误!");
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或者密码错误" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *isOK = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
            [alert addAction:isOK];
            [self presentViewController:alert animated:YES completion:nil];
        }
        
    }];
    
  
}

//找回密码按钮点击事件
- (void)didClickRetrievePwdBtn:(UIButton *)sender
{
//    RetrievePasswordViewController *retrievePwdVC = [[RetrievePasswordViewController alloc] init];
//    [self.navigationController pushViewController:retrievePwdVC animated:YES];
}

- (void)didClickRegisterBtn:(UIButton *)sender
{
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    if (self.navigationController) {
        [self.navigationController pushViewController:registerVC animated:YES];
    }else{
        [self presentViewController:registerVC animated:YES completion:nil];
    }
    

    
}




//- (void)didClickLeftBtn:()


@end
